package org.strongme.modules.project.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.strongme.common.web.BaseController;
import org.strongme.modules.project.entity.OpGuide;
import org.strongme.modules.project.entity.Project;
import org.strongme.modules.project.entity.Tz;
import org.strongme.modules.project.service.OpGuideService;
import org.strongme.util.StringUtils;

import com.google.common.collect.Lists;

@Controller
@RequestMapping("project/opGuide")
public class OpGuideController extends BaseController{
	
	@Autowired
	private OpGuideService opGuideService;
	
	@ModelAttribute("opGuide")
	public OpGuide get(@RequestParam(required=false) String id, @RequestParam(required=false)String projectId, @RequestParam(required=false) String suoxie) {
		OpGuide opGuide = null;
		if (StringUtils.isNotBlank(id)){
			opGuide = opGuideService.get(new OpGuide(id));
		}else if(StringUtils.isNotBlank(projectId) && StringUtils.isNoneBlank(suoxie)) {
			opGuide = new OpGuide();
			Project p = new Project(projectId);p.setSuoxie(suoxie);
			opGuide.setProject(p);
		}else{
			opGuide = new OpGuide();
		}
		return opGuide;
	}

	@RequestMapping(value = {"index", ""})
	public String index() {
		return "modules/project/opGuide/opGuideIndex.jsp";
	}
	
	@RequestMapping(value = {"list"})
	public String list(OpGuide opGuide, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<OpGuide> list = opGuideService.findList(opGuide);
        model.addAttribute("list", list);
        model.addAttribute("opGuide", opGuide);
		return "modules/project/opGuide/opGuideList.jsp";
	}
	
	@RequestMapping(value = "form")
	public String form(OpGuide opGuide, Model model) {
		if (opGuide.getNotnew() != null && "yes".equals(opGuide.getNotnew())) {
			opGuide.setId(null);
		}
		if(opGuide.getId()==null || "".equals(opGuide.getId())) {
			//设置编号
			Integer count = opGuideService.checkForCountThisProject(opGuide)+1;
			String bh = opGuide.getProject().getSuoxie()+"-RTZDS-";
			if(count<10) {
				bh += "00"+count;
			}else if(count>10 && count<100) {
				bh += "0"+count;
			}else {
				bh += count;
			}
			opGuide.setCzzdsbh(bh);
			opGuide.setGdl("5mA");
			opGuide.setGygcjbbh("SXKS/WT-RT-2015");
		}

		if(opGuide!=null&&(opGuide.getGg()==null||"".equals(opGuide.getGg()))) {
			opGuide.setGg("Φ ×");
		}
		model.addAttribute("opGuide", opGuide);
		return "modules/project/opGuide/opGuideForm.jsp";
	}
	
	@RequestMapping(value = "save")
	public String save(OpGuide opGuide, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, opGuide)){
			return form(opGuide, model);
		}
		if(StringUtils.isNotBlank(opGuide.getGg())) {
			String gg  = opGuide.getGg();
			gg = gg.replaceAll(" ","");
			opGuide.setGg(gg);
		}
		opGuideService.save(opGuide);
		addMessage(redirectAttributes, "保存作业说明书'" + opGuide.getCzzdsbh() + "'成功");
		return "redirect:/project/opGuide/list?opGuide.project.id="+opGuide.getProject().getId();
	}

	@RequestMapping(value = "delete")
	public String delete(OpGuide opGuide, RedirectAttributes redirectAttributes) {
		opGuideService.delete(opGuide);
		addMessage(redirectAttributes, "删除操作指导书记录成功");
		return "redirect:/project/opGuide/list?opGuide.project.id="+opGuide.getProject().getId();
	}

	@RequestMapping(value= "listOGByGgAndTzfs")
	@ResponseBody
	public List<OpGuide> getOGByProject(OpGuide opGuide) {
		if(opGuide==null||opGuide.getProject()==null||StringUtils.isBlank(opGuide.getProject().getId())||StringUtils.isBlank(opGuide.getGg())||StringUtils.isBlank(opGuide.getTzfs())) {
			return Lists.newArrayList();
		}else {
			List<OpGuide> result = opGuideService.getOGByProjectGG_Tzfs(opGuide);
			if(result==null) result = Lists.newArrayList();
			return result;
		}
	}
	
	@RequestMapping("loadOGByProGG")
	@ResponseBody
	public OpGuide loadOGByProAndGG(OpGuide opGuide) {
		return opGuideService.getOGByProAndGG(opGuide);
	}
	
}
